﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LibBinary.FileData
{
    public class FieldData : AbstractFileData
    {
        public string FieldName;
        public AbstractFileData Data;

        public FieldData(AbstractFileData parent)
            : base(parent)
        {
        }

        public override AbstractFileData GetChild(int index)
        {
            if (index != 0)
            {
                throw new ReadingFormatException("Wrong index in FieldData.");
            }
            return Data;
        }

        public override string ToString()
        {
            string ret = FieldName == null ? "<unnamed> : " : (FieldName + " : ");
            ret += Data == null ? "<empty>" : Data.ToString();
            return ret;
        }
    }

    public static class AbstractDataFieldExt
    {
        public static T FieldAs<T>(this AbstractFileData data) where T : AbstractFileData
        {
            return data.As<FieldData>().Data.As<T>();
        }
    }
}
